# 乳腺癌案例

import pandas as pd
import numpy as np

df = pd.read_csv('datasets_ML2/breast-cancer-wisconsin.data', header=None)
print(df.head(), df.info())
print(df[6].value_counts())
# 去除？数据
df.replace('?', np.NaN, inplace=True)
df.dropna(inplace=True)
print('----------------')
print(df[6].value_counts())

import matplotlib.pyplot as plt
import seaborn as sn

ax = sn.boxplot(data=df)
plt.show()

print(df.info())
# 正常情况下，分类要变成 2-->0，4-->1 的方式进行处理才可以
df[10] = df[10].map({2: 0, 4: 1})
# 查看类别的频数统计
print(df[10].value_counts())

# 类别不平衡   过采样
# 从类别1当中随机抽样444-239条数据
df1 = df[df[10]==1].sample(444-239)
# 合并df和df1就能保证类别之间的数量平衡
df = pd.concat([df, df1], axis=0)

print('-----------------')
print(df[10].value_counts())

outliers=np.abs((df[0]-df[0].mean()) > (3*df[0].std()))
print("outliers： ", len(df[outliers.values]))

goodpoints=np.abs((df[0]-df[0].mean()) <= (3*df[0].std()))
print("goodpoints： ", len(df[goodpoints.values]))
df = df[goodpoints.values]

# 切分x，y
y = df[[10]]
del df[10]
x = df

x = x.values
y = y.values

# 特征缩放
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
x = std.fit_transform(x)

# data visualization
U, S, V = np.linalg.svd(x)
k = 2
P = V[:k, :].T
print("P: ", P)
z = np.dot(x, P)
print("np.argmin(z[:, 1]): ", np.argmin(z[:, 1]))
plt.scatter(z[:, 0], z[:, 1], c=y)
plt.show()

# 切分训练集和测试集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

# 创建逻辑回归模型
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(C=10)# 正则项为0.1的操作

model.fit(x_train, y_train)
y_ = model.predict(x_test)

# 模型预测的概率，是两列数据，分别是负样本概率和正样本概率，加和值为1
# print(model.predict_proba(x_test))

# 模型评测
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report, roc_curve, roc_auc_score, confusion_matrix

print('准确率:', accuracy_score(y_test, y_))
print('准确率：', model.score(x_test, y_test))
print('精确率:', precision_score(y_test, y_))
print('召回率：', recall_score(y_test, y_))
print('f1:', f1_score(y_test, y_))

print('分类报告')
print(classification_report(y_test, y_))
print('混淆矩阵')
print(confusion_matrix(y_test, y_))

# roc曲线和AUC得分
fpr, tpr, th = roc_curve(y_test, model.predict_proba(x_test)[:, -1:])
import matplotlib.pyplot as plt
plt.plot(fpr, tpr)
plt.plot([0, 1], [0, 1], 'k--')
plt.show()

auc_score = roc_auc_score(y_test, model.predict_proba(x_test)[:, -1:])
print(auc_score)
